<?php

namespace app\api\controller\promotion;

use app\api\controller\Controller;
use app\common\model\plus\agent\Setting;
use app\common\model\plus\agent\SupplierSetting;
use app\common\model\plus\coupon\Coupon as CouponModel;

class User extends Controller
{
    public function getPrizeList()
    {
        $list = [];
        for ($i = 0; $i < 20; $i++) {
            $list[] = ['name' => $this->generateNickname(), 'value' => $this->generateValue()];
        }
        return $this->renderSuccess('', compact('list'));
    }

    private function generateNickname()
    {
        $nicknames = [
            "糖果大盗",
            "梦幻旋律",
            "快乐小丸子",
            "超级披萨侠",
            "星际拼图王",
            "茶叶蛋特工",
            "雷电闪客",
            "跳舞的火龙果",
            "暗夜小猫侠",
            "果汁忍者",
            "闪光的奇迹",
            "小小蓝天使",
            "魔法小辣椒",
            "爆笑大白菜",
            "咖啡因小怪兽",
            "玩具总动员",
            "蜜糖小魔女",
            "钢铁侠的表弟",
            "彩虹小飞侠",
            "奇异果超人",
            "雷霆火箭猫",
            "太空披萨使者",
            "蓝色海豚梦",
            "旋风小呆瓜",
            "糖果巡警队",
            "快乐旋律师",
            "魔法蘑菇精",
            "摇滚小企鹅",
            "虚空小狐狸",
            "霓虹星辰梦",
            "幸运小狸猫",
            "魔法雪花精",
            "软绵绵大白",
            "梦幻彩虹风",
            "小小星际舰长",
            "炸弹冰淇淋",
            "跳跃的奇迹",
            "美梦小猫仙",
            "音乐蝴蝶梦",
            "果冻甜心宝",
            "电光火石狂魔",
            "梦幻宇航员",
            "喜笑颜开小丸子",
            "独角兽奇迹",
            "彩虹梦游者",
            "旋风小快递",
            "摇滚狂热者",
            "幽灵舞者",
            "翻滚果冻怪",
            "星际蓝天使",
            "飞天小猪侠",
            "极光小精灵",
            "追梦小火箭",
            "奇幻糖果王",
            "蜜蜂舞者",
            "冰雪甜心仙",
            "小小海盗王",
            "夜空流星雨",
            "闪耀音符精灵",
            "草原小精灵"
        ];

        // 从数组中随机选择一个元素
        $randomNickname = $nicknames[array_rand($nicknames)];

        return $randomNickname;
    }

    private function generateValue()
    {
        // 生成一个数值
        return bcround(rand(10, 80), 2);
    }

    /**
     * 获取邀请列表
     * @return \think\response\Json
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author joey
     * @date 2024/2/29 17:33
     */
    public function getinvitelist()
    {
        $params = array_merge($this->postData(), [
            'is_delete' => 0,
            'user_type' => 1
        ]);
        // 当前用户信息
        $user     = $this->getUser();
        $list     = \think\facade\Db::table('jjjshop_user')
            ->alias('u')
            ->field('u.*,c.name,c.reduce_price,c.discount,c.coupon_type,p.user_promoter_coupon_id as upci')
            ->join(['jjjshop_user_promoter_coupon' => 'p'], 'p.referee_id=u.referee_id and p.user_id=u.user_id', 'LEFT')
            ->join(['jjjshop_coupon' => 'c'], 'c.coupon_id=p.coupon_id', 'LEFT')
            ->where(['u.referee_id' => $user['user_id']])
            ->order(['u.create_time' => 'desc'])
            ->paginate($params);
        $total = 0;
        foreach ($list as $item) {
            $total += $item['reduce_price'];
        }
        return $this->renderSuccess('', compact('list','total'));
    }

    public function getinvitesetting(){
        // 20240524 Mr.Z 商户可个性化设置
        $Shopinfo = $this->getUserCache();
        $settings = SupplierSetting::getItem('basic',$Shopinfo['shop'],true);
        if(!$settings){
            $settings = Setting::getItem('basic');
        }
        $fgcoupon = $settings['first_give_coupon'];
        $conptotal = 0;
        $conplist = [];
        if($fgcoupon==1&&isset($settings['become_coupon_ids'])) {
            $conplist = (new CouponModel())->where('coupon_id', 'in', $settings['become_coupon_ids'])->select();
            foreach ($conplist as $key => $cvalue) {
                $conptotal += $cvalue['reduce_price'];
            }
        }
        return $this->renderSuccess('', compact('conptotal','conplist','fgcoupon','settings'));
    }
}